package ui;


import config.ConfigFactory;
import config.GameConfig;
import dto.GameDto;

import javax.swing.*;
import java.awt.*;
import java.util.zip.ZipEntry;

abstract public class Layer {
    protected  static  final  int  PADDING;
    protected  static  final  int  SIZE;
    protected   static Image WINDOW_IMG = new ImageIcon("graphics/window/Window.png").getImage();
    protected  static int WINDOW_W = WINDOW_IMG.getWidth(null);
    protected  static int WINDOW_H = WINDOW_IMG.getHeight(null);

    /**
     * 数字图片
     */
    protected    static Image IMG_NUMBER = new ImageIcon("graphics/string/num.png").getImage();
    protected static  final  int IMG_NUMBER_W = IMG_NUMBER.getWidth(null)/10;//数字切片的欢度
    protected static  final  int IMG_NUMBER_H = IMG_NUMBER.getHeight(null);//数字切片的宽度

    static {
        GameConfig cfg = ConfigFactory.getGameConfig();
        SIZE = cfg.getWindowSize();
        PADDING = cfg.getPadding();

    }
    /**
     * 窗口左上角X坐标
     */
    protected int x;
    /**
     * 窗口左上角y坐标
     */
    protected int y;
    /**
     * 窗口宽度
     */
    protected int w;
    /**
     *窗口高度
     */
    protected int h;

    protected   GameDto dto ;

    public void setDto(GameDto dto) {
        this.dto = dto;
    }

    protected Layer(int x, int y, int w, int h) {
        this.x = x;
        this.y = y;
        this.w = w;
        this.h = h;
    }

    /**
     * 绘制窗口
     * @param g
     */
    protected void createWindow (Graphics g) {
        //左上
        g.drawImage(WINDOW_IMG,x,y,x+SIZE,y+SIZE,0,0,SIZE,SIZE,null);
        //中上
        g.drawImage(WINDOW_IMG,x+SIZE,y,x+w-SIZE,y+SIZE,SIZE,0,WINDOW_W-SIZE,SIZE,null);
        //右上
        g.drawImage(WINDOW_IMG,x+w-SIZE,y,x+w,y+SIZE,WINDOW_W-SIZE,0,WINDOW_W,SIZE,null);
        //左中
        g.drawImage(WINDOW_IMG,x,y+SIZE,x+SIZE,y+h-SIZE,0,SIZE,SIZE,WINDOW_H-SIZE,null);
        //中
        g.drawImage(WINDOW_IMG,x+SIZE,y+SIZE,x+w-SIZE,y+h-SIZE,SIZE,SIZE,WINDOW_W-SIZE,WINDOW_H-SIZE,null);
        //右中
        g.drawImage(WINDOW_IMG,x+w-SIZE,y+ SIZE,x+w,y+h-SIZE,WINDOW_W-SIZE,SIZE,WINDOW_W,WINDOW_H-SIZE,null);
        //左下
        g.drawImage(WINDOW_IMG,x,y+h-SIZE,x+SIZE,y+h,0,WINDOW_H-SIZE,SIZE,WINDOW_H,null);
        //中下
        g.drawImage(WINDOW_IMG,x+SIZE,y+h-SIZE,x+w-SIZE,y+h,SIZE,WINDOW_H-SIZE,WINDOW_W-SIZE,WINDOW_H,null);
        //右下
        g.drawImage(WINDOW_IMG,x+w-SIZE,y+h-SIZE,x+w,y+h,WINDOW_W-SIZE,WINDOW_H-SIZE,WINDOW_W,WINDOW_H,null);
    }



    /**
     *显示数字的方法
     */
    protected   void drawNumberLefetPad(int x, int y, int num,int maxBit,Graphics g) {
        //把数字的num 把每一位数字取出
        String strNum = Integer.toString(num);
        for (int i = 0; i < maxBit ; i++) {
            if (maxBit - i <= strNum.length()) {
                int idx = i - maxBit + strNum.length();
                int bit = strNum.charAt(idx) - '0';
                g.drawImage(IMG_NUMBER,
                        this.x + x + IMG_NUMBER_W * i , this.y + y,this.x + x + IMG_NUMBER_W * (i + 1),this.y + y + IMG_NUMBER_H,
                        bit * IMG_NUMBER_W,0,(bit + 1) * IMG_NUMBER_W , IMG_NUMBER_H,null);
            }


        }


    }

    abstract public void paint(Graphics g);

}
